package sy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;





import sy.dao.SysDivisionDaoI;
import sy.model.SysDivision;
import sy.service.SysDivisionServiceI;
import sy.util.Tree;
import sy.util.ZtreeNode;

@Service("sysDivisionService")
public class SysDivisionServiceImpl implements SysDivisionServiceI {

	private static final Logger logger = Logger.getLogger(SysDivision.class);
	private SysDivisionDaoI sysDivisionDao;


	public SysDivisionDaoI getSysDivisionDao() {
		return sysDivisionDao;
	}

	@Autowired
	public void setSysDivisionDao(SysDivisionDaoI sysDivisionDao) {
		this.sysDivisionDao = sysDivisionDao;
	}

	@Override
	public Serializable save(SysDivision sysDivision) {

		return sysDivisionDao.save(sysDivision);
	}

	@Override
	public void remove(SysDivision sysDivision) {
		// TODO Auto-generated method stub
		sysDivisionDao.remove(sysDivision);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#update(sy.model.SysDivision)
	 */
	@Override
	public void update(SysDivision sysDivision) {
		// TODO Auto-generated method stub
		sysDivisionDao.update(sysDivision);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#list()
	 */
	@Override
	public List<SysDivision> list() {
		// TODO Auto-generated method stub
		return sysDivisionDao.list();
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#getSysDivisionById(java.lang.String)
	 */
	@Override
	public SysDivision getSysDivisionById(String id) {
		// TODO Auto-generated method stub
		return sysDivisionDao.getSysDivisionById(id);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#listSysDivisionByKey(java.lang.String)
	 */
	@Override
	public List<SysDivision> listSysDivisionByKey(String key) {
		// TODO Auto-generated method stub
		return sysDivisionDao.listSysDivisionByKey(key);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#listSysDivisionTree()
	 */
	@Override
	public List<Tree> listSysDivisionTree() {
		// TODO Auto-generated method stub
		List<Tree> trlist = new LinkedList();
		List<SysDivision> allList = sysDivisionDao.list();
		List<SysDivision> firstLeafs = new ArrayList<SysDivision>();
		List<SysDivision> secondLeafs = new ArrayList<SysDivision>();
		if(!allList.isEmpty()){
			for(SysDivision all: allList){
				if("CN".equals(all.getSysDivision().getId())){
					firstLeafs.add(all);//一级子节点
				}else{
					secondLeafs.add(all);//二级子节点
				}
			}
		}
		Tree parent = new Tree();//树的父节点
		parent.setNoteId("0");
		parent.setText("中国");
		parent.getState().setExpanded(true);
		trlist.add(parent);
		List<Tree> firstLeafTree = new ArrayList<Tree>();
		for (Iterator firlIterator = firstLeafs.iterator(); firlIterator.hasNext();) {
			SysDivision firstLeaf = (SysDivision)firlIterator.next();
			Tree t = new Tree();
			t.setNoteId(firstLeaf.getId());
			t.setText(firstLeaf.getName());
			t.getState().setExpanded(true);
			firstLeafTree.add(t);
			int count = 0;
			for (Iterator seclIterator = secondLeafs.iterator(); seclIterator.hasNext();){
				SysDivision secondLeaf = (SysDivision)seclIterator.next();
				count++;
				if (firstLeaf.getId().equals(secondLeaf.getSysDivision().getId())) {
					Tree children = new Tree();
					children.setNoteId(secondLeaf.getId());
					children.setText(secondLeaf.getName());
					children.getState().setExpanded(true);
					children.setNodes(null);
					t.getNodes().add(children);
				}
			}
			if (count == 0) {
				t.setNodes(null);
			}
		}
		parent.setNodes(firstLeafTree);
		return trlist;
	}

	/* (non-Javadoc)
	 * @see sy.service.SysDivisionServiceI#listSysDivisionZTree()
	 */
	@Override
	public List<ZtreeNode> listSysDivisionZTree() {
		// TODO Auto-generated method stub
		List<ZtreeNode> ztrlist = new LinkedList();
		List<SysDivision> allList = sysDivisionDao.list();
		List<SysDivision> firstLeafs = new ArrayList<SysDivision>();
		List<SysDivision> secondLeafs = new ArrayList<SysDivision>();
		if(!allList.isEmpty()){
			for(SysDivision all: allList){
				if(all.getSysDivision()!=null &&"CN".equals(all.getSysDivision().getId())){
					firstLeafs.add(all);//一级子节点
				}else{
					secondLeafs.add(all);//二级子节点
				}
			}
		}
		ZtreeNode parent = new ZtreeNode();//树的父节点
		parent.setId("0");
		parent.setName("中国");
		parent.setOpen(true);
		parent.setpId(null);
		ztrlist.add(parent);
		for (Iterator firlIterator = firstLeafs.iterator(); firlIterator.hasNext();) {
			SysDivision firstLeaf = (SysDivision)firlIterator.next();
			ZtreeNode t = new ZtreeNode();
			t.setId(firstLeaf.getId());
			t.setName(firstLeaf.getName());
			t.setOpen(true);
			t.setpId("0");
			ztrlist.add(t);
			for (Iterator seclIterator = secondLeafs.iterator(); seclIterator.hasNext();){
				SysDivision secondLeaf = (SysDivision)seclIterator.next();
				if (secondLeaf.getSysDivision()!=null && firstLeaf.getId().equals(secondLeaf.getSysDivision().getId())) {
					ZtreeNode children = new ZtreeNode();
					children.setId(secondLeaf.getId());
					children.setName(secondLeaf.getName());
					children.setOpen(false);
					children.setpId(firstLeaf.getId());
					ztrlist.add(children);
				}
			}
		}
		return ztrlist;
	}

}
